﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NyBio.Ctrl;
using NyBio.Model;

namespace NyBio
{
    public partial class MainForm : Form
    {
        UserCtrl userCtrl;
        MovieCtrl movieCtrl;
        ScheduleCtrl scheduleCtrl;
        RoomCtrl roomCtrl;
        TicketCtrl ticketCtrl;
        SeatCtrl seatCtrl;

        private List<User> userList;
        private List<Movie> movieList;
        private List<User> reservationUserList;
        private List<Schedule> scheduleList;
        private List<Ticket> ticketList;
        private List<Room> roomList;
        private List<Schedule> reservationScheduleList;
        private List<SeatDropDown> seatDropDownList;
        private List<Seat> seatResultList;
        private List<Movie> reservationMovieList; 
        private List<Schedule> reservationTimeList;

        public MainForm()
        {
            InitializeComponent();

            userCtrl = new UserCtrl();
            movieCtrl = new MovieCtrl();
            scheduleCtrl = new ScheduleCtrl();
            roomCtrl = new RoomCtrl();
            ticketCtrl = new TicketCtrl();
            seatCtrl = new SeatCtrl();

            txtUserSearch.Text = "Brugernavn..";
            txtReservationMemberSearch.Text = "Brugernavn..";
            txtMovieSearch.Text = "Titel..";
            txtRoomSearch.Text = "Navn..";
            txtScheduleSearch.Text = "Film..";

            reservationMovieList = new List<Movie>();
            reservationTimeList = new List<Schedule>();
            userList = new List<User>();
            movieList = new List<Movie>();
            reservationUserList = new List<User>();
            scheduleList = new List<Schedule>();
            ticketList = new List<Ticket>();
            roomList = new List<Room>();
            reservationScheduleList = new List<Schedule>();

            bindReservationMovies();
            bindReservationTime();
            bindReservationSeat();



        }

        private void bindReservationMovies() //
        {
            ddlReservationMovies.Items.Clear();

            reservationMovieList = movieCtrl.getMovies();

            ddlReservationMovies.DataSource = reservationMovieList;
            ddlReservationMovies.ValueMember = "_id";
            ddlReservationMovies.DisplayMember = "_title";
        }

        private void bindReservationTime()
        {
            reservationTimeList = scheduleCtrl.getScheduleByMovieId(reservationMovieList[ddlReservationMovies.SelectedIndex]._id);

            lblReservationMovieTimes.DataSource = reservationTimeList; // Convert.ToInt32(ddlReservationMovies.SelectedValue)
            lblReservationMovieTimes.ValueMember = "_id";
            lblReservationMovieTimes.DisplayMember = "_time";
        }

        private void bindReservationSeat()
        {
            ddlReservationSeat.SelectedIndex = -1;
            seatDropDownList = new List<SeatDropDown>();

            seatResultList = seatCtrl.getSeatsByRoom(reservationTimeList[lblReservationMovieTimes.SelectedIndex]._id); // scheduleList[lblReservationMovieTimes.SelectedIndex]._roomId

            foreach (Seat item in seatResultList)
            {
                seatDropDownList.Add(new SeatDropDown(item._id, "Række " + item._row + ", Kolonne " + item._col));
            }

            ddlReservationSeat.DataSource = seatDropDownList;
            ddlReservationSeat.ValueMember = "_id";
            ddlReservationSeat.DisplayMember = "_text";
        }

        private void lblMoviesSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Movie movie = movieList[lblMoviesSearch.SelectedIndex];

                txtMovieDescription.Text = movie._description;
                txtMovieDirector.Text = movie._director;
                txtMovieGenre.Text = movie._genre;
                txtMovieLength.Text = movie._length.ToString();
                txtMovieReview.Text = movie._review;
                txtMovieTitle.Text = movie._title;

                if (lblMoviesSearch.SelectedIndex != -1)
                {
                    btnMovieDelete.Enabled = true;
                    btnMovieUpdate.Enabled = true;
                }
                else
                {
                    btnMovieDelete.Enabled = false;
                    btnMovieUpdate.Enabled = false;
                }
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void lblMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                User user = userList[lblUserSearch.SelectedIndex];

                txtUserEmail.Text = user._email;
                txtUserName.Text = user._name;

                chkUserTenth.Checked = user._filmRabat;
                chkUserSenior.Checked = user._seniorRabat;
                chkUserStudent.Checked = user._studieRabat;
            }
            catch (Exception)
            {
                lblResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieInsert_Click(object sender, EventArgs e)
        {
            try
            {
                movieCtrl.insertMovie(new Movie(txtMovieTitle.Text, txtMovieDescription.Text, txtMovieLength.Text, txtMovieReview.Text, txtMovieDirector.Text, txtMovieGenre.Text));

                lblMovieResult.Text = "Film oprettet.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                movieCtrl.updateMovie(new Movie(movieList[lblMoviesSearch.SelectedIndex]._id, txtMovieTitle.Text, txtMovieDescription.Text, txtMovieLength.Text, txtMovieReview.Text, txtMovieDirector.Text, txtMovieGenre.Text));

                movieList[lblMoviesSearch.SelectedIndex]._title = txtMovieTitle.Text;

                lblMovieResult.Text = "Film redigeret.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = lblMoviesSearch.SelectedIndex;

                movieCtrl.deleteMovie(movieList[selectedIndex]._id);
                movieList.RemoveAt(selectedIndex);
                lblMoviesSearch.Items.RemoveAt(selectedIndex);

                lblMoviesSearch.SelectedIndex = -1;

                lblMovieResult.Text = "Film fjernet.";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnRoomSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblRoomSearch.Items.Clear();
                roomList.Clear();

                roomList = roomCtrl.getRoomSearch(txtRoomSearch.Text);

                foreach (Room item in roomList)
                {
                    lblRoomSearch.Items.Add(item._name);
                }

                lblRoomResult.Text = "Medlem orettet";
            }
            catch (Exception)
            {
                lblRoomResult.Text = "Der opstod en fejl.";
            }
        }

        private void txtReservationMemberSearch_Click(object sender, EventArgs e)
        {
            txtReservationMemberSearch.Text = "";
        }

        private void txtReservationSearch_Click(object sender, EventArgs e)
        {
            txtReservationSearch.Text = "";
        }

        private void btnScheduleDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = lstScheduleSearch.SelectedIndex;

                scheduleCtrl.deleteSchedule(scheduleList[lstScheduleSearch.SelectedIndex]._id);
                scheduleList.RemoveAt(selectedIndex);
                lstScheduleSearch.Items.RemoveAt(selectedIndex);

                lstScheduleSearch.SelectedIndex = -1;

                lblScheduleResult.Text = "Reservering fjernet.";
            }
            catch (Exception)
            {
                lblScheduleResult.Text = "Der opstod en fejl.";
            }
        }


        private void bntScheduleInsert_Click(object sender, EventArgs e)
        {
            try
            {
                scheduleCtrl.insertschedule(new Schedule(Convert.ToDateTime(txtScheduleTime.Text), Convert.ToInt32(ddlScheduleMovie.SelectedValue), Convert.ToInt32(ddlScheduleRoom.SelectedValue), Convert.ToDecimal(txtSchedulePrice.Text)));

                lblScheduleResult.Text = "Reservering oprettet.";
            }
            catch (Exception)
            {
                lblScheduleResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnMovieSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblMoviesSearch.Items.Clear();
                movieList.Clear();

                movieList = movieCtrl.getMovieSearch(txtMovieSearch.Text);

                foreach (Movie item in movieList)
                {
                    lblMoviesSearch.Items.Add(item._title);
                }

                lblMovieResult.Text = "";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }
        }
        private void btnReservationSearch_Click(object sender, EventArgs e)
        {
            //try
            //{
            lstReservationSearch.Items.Clear();

            ticketList = ticketCtrl.getTickets();

            foreach (Ticket item in ticketList)
            {
                lstReservationSearch.Items.Add(item._id);

            }

            //lblScheduleResult.Text = "";
            //}
            //catch (Exception)
            //{
            //    lblScheduleResult.Text = "Der opstod en fejl.";
            //}
        }
        private void btnMemberSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblReservationMemberSearch.Items.Clear();
                reservationUserList.Clear();

                reservationUserList = userCtrl.getMemberSearch(txtReservationMemberSearch.Text);

                foreach (User item in reservationUserList)
                {
                    lblReservationMemberSearch.Items.Add(item._name);
                }

                lblReservationResult.Text = "";
            }
            catch (Exception)
            {
                lblReservationResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnScheduleSearch_Click(object sender, EventArgs e)
        {
            try
            {
                lblMoviesSearch.Items.Clear();
                movieList.Clear();

                movieList = movieCtrl.getMovieSearch(txtMovieSearch.Text);

                foreach (Movie item in movieList)
                {
                    lblMoviesSearch.Items.Add(item._title);
                }

                lblMovieResult.Text = "";
            }
            catch (Exception)
            {
                lblMovieResult.Text = "Der opstod en fejl.";
            }

            try
            {
                lstScheduleSearch.Items.Clear();
                scheduleList.Clear();

                scheduleList = scheduleCtrl.getScheduleByMovieTitle(txtScheduleSearch.Text);

                foreach (Schedule item in scheduleList)
                {

                    lstScheduleSearch.Items.Add(item._time);
                }
            }
            catch (Exception)
            {
                lblScheduleResult.Text = "Der opstod en fejl.";
            }
        }

        private void txtUserSearch_Click(object sender, EventArgs e)
        {
            txtUserSearch.Text = "";
        }

        private void txtRoomSearch_Click(object sender, EventArgs e)
        {
            txtRoomSearch.Text = "";
        }

        private void txtScheduleSearch_Click(object sender, EventArgs e)
        {
            txtScheduleSearch.Text = "";
        }

        private void txtMovieSearch_Click(object sender, EventArgs e)
        {
            txtMovieSearch.Text = "";
        }

        private void btnReservationDelete_Click(object sender, EventArgs e)
        {

            try
            {
                int selectedIndex = lstReservationSearch.SelectedIndex;

                ticketCtrl.removeTicket(ticketList[selectedIndex]._id);
                ticketList.RemoveAt(selectedIndex);
                lstReservationSearch.Items.RemoveAt(selectedIndex);

                lstReservationSearch.SelectedIndex = -1;

                lstReservationSearch.Text = "Reservation fjernet.";
            }
            catch (Exception)
            {
                lstReservationSearch.Text = "Der opstod en fejl eller to.";
            }
        }

        private void tabMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Sal.SelectedIndex == 0)
            {
                ddlReservationMovies.Items.Clear();
                ddlReservationSeat.Items.Clear();

                ddlReservationMovies.DataSource = movieCtrl.getMovies();
                ddlReservationMovies.DisplayMember = "title";
                ddlReservationMovies.ValueMember = "xpMovie_id";

                ddlReservationSeat.DataSource = seatCtrl.getSeatsByRoom(scheduleList[lblReservationMovieTimes.SelectedIndex]._roomId);
                ddlReservationSeat.DisplayMember = "title";
                ddlReservationSeat.ValueMember = "xpSeat_id";
            }
            else if (Sal.SelectedIndex == 2)
            {
                ddlScheduleMovie.Items.Clear();
                ddlScheduleRoom.Items.Clear();

                ddlScheduleMovie.DataSource = movieCtrl.getMovies();
                ddlScheduleMovie.DisplayMember = "title";
                ddlScheduleMovie.ValueMember = "xpMovie_id";

                ddlScheduleRoom.DataSource = roomCtrl.getRooms();
                ddlScheduleRoom.DisplayMember = "name";
                ddlScheduleRoom.ValueMember = "xpRoom_id";
            }
        }

        private void btnRoomInsert_Click(object sender, EventArgs e)
        {
            try
            {

                roomCtrl.insertRoom(new Room(txtRoomName.Text, Convert.ToInt32(txtRoomRows.Text), Convert.ToInt32(txtRoomColumns.Text)));

                Room room = roomCtrl.getLatestRoom();

                int row = 1;
                int column = 1;

                for (int i = 0; i < room._numberOfRows * room._numberOfColumns; i++)
                {
                    seatCtrl.insertSeat(new Seat(Convert.ToInt32(txtRoomRows.Text), Convert.ToInt32(txtRoomColumns.Text), false));

                    if (column == room._numberOfColumns)
                    {
                        column = 1;
                        row++;
                    }
                    else
                    {
                        column++;
                    }
                }

                lblRoomResult.Text = "Sal oprettet.";
            }
            catch (Exception)
            {
                lblRoomResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnScheduleInsert_Click(object sender, EventArgs e)
        {
            try
            {
                scheduleCtrl.insertschedule(new Schedule(Convert.ToDateTime(txtScheduleTime.Text), reservationMovieList[ddlReservationMovies.SelectedIndex]._id, Convert.ToInt32(ddlScheduleRoom.SelectedValue), Convert.ToDecimal(txtSchedulePrice.Text)));

                lblScheduleResult.Text = "Reservering oprettet.";
            }
            catch (Exception)
            {
                lblScheduleResult.Text = "Der opstod en fejl.";
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }


        private void btnScheduleUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                scheduleCtrl.updateSchedule(new Schedule(Convert.ToDateTime(txtScheduleTime.Text), Convert.ToInt32(ddlScheduleMovie.SelectedValue), Convert.ToInt32(ddlScheduleRoom.SelectedValue), Convert.ToDecimal(txtSchedulePrice.Text)));

                lblScheduleResult.Text = "Reservering rettet.";
            }
            catch (Exception)
            {
                lblScheduleResult.Text = "Der opstod en fejl.";
            }
        }

        private void lstReservationSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstReservationSearch.SelectedIndex != -1)
            {
                btnReservationDelete.Enabled = true;
                btnReservationUpdate.Enabled = true;
            }
            else
            {
                btnReservationDelete.Enabled = false;
                btnReservationUpdate.Enabled = false;
            }
        }

        private void lstShedulesearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstScheduleSearch.SelectedIndex != -1)
            {
                btnScheduleDelete.Enabled = true;
                btnScheduleUpdate.Enabled = true;
            }
            else
            {
                btnScheduleDelete.Enabled = false;
                btnScheduleSearch.Enabled = false;
            }
        }

        private void btnUserInsert_Click(object sender, EventArgs e)
        {
            try
            {
                userCtrl.insertUser(new User(txtUserEmail.Text, txtUserName.Text, chkUserSenior.Checked, chkUserTenth.Checked, chkUserStudent.Checked));

                lblRoomResult.Text = "Medlem gemt.";
            }
            catch (Exception)
            {
                lblRoomResult.Text = "Der opstod en fejl.";
            }
        }

        private void btnReservationInsert_Click(object sender, EventArgs e)
        {
            //try
            //{
            decimal calculatedPrice = reservationTimeList[lblReservationMovieTimes.SelectedIndex]._price;
                User selectedUser = reservationUserList[lblReservationMemberSearch.SelectedIndex];

                if (selectedUser._studieRabat == true)
                {
                    calculatedPrice = (calculatedPrice / 100) * 10;

                }
                else if (selectedUser._seniorRabat == true)
                {
                    calculatedPrice = (calculatedPrice / 100) * 30;
                }


               ticketCtrl.insertTicket(new Ticket(reservationTimeList[lblReservationMovieTimes.SelectedIndex]._id, Convert.ToInt32(ddlReservationSeat.SelectedValue), selectedUser._id, calculatedPrice));


                lblReservationResult.Text = "Reservation opretet.";
            //}
            //catch (Exception)
            //{
            //    lblReservationResult.Text = "Der opstod en fejl.";
            //}

        }




        private void lstReservationSearch_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void btnReservationUpdate_Click(object sender, EventArgs e)
        {

            //try
            //{
            decimal calculatedPrice = reservationTimeList[lblReservationMovieTimes.SelectedIndex]._price;
                User selectedUser = reservationUserList[lblReservationMemberSearch.SelectedIndex];

                if (selectedUser._studieRabat == true)
                {
                    calculatedPrice = (calculatedPrice / 100) * 10;

                }
                else if (selectedUser._seniorRabat == true)
                {
                    calculatedPrice = (calculatedPrice / 100) * 30;
                }

                ticketCtrl.updateTicket(new Ticket(ticketList[lstReservationSearch.SelectedIndex]._id, reservationTimeList[lblReservationMovieTimes.SelectedIndex]._id, Convert.ToInt32(ddlReservationSeat.SelectedValue), selectedUser._id, calculatedPrice));

                txtMovieTitle.Text = ticketList[lstReservationSearch.SelectedIndex]._id.ToString();

                lblReservationResult.Text = "Reservation redigeret.";
            //}
            //catch (Exception)
            //{
            //    lblReservationResult.Text = "Der opstod en fejl.";
            //}

        }
        private void ddlReservationMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindReservationTime();
        }

        private void lblReservationMovieTimes_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindReservationSeat();
        }

    }
}
        
        


    
